<template>
	<view>
		<!-- 列表项 -->
		<view v-for="(item,index) in goodsList" :key="index" @click="GoDetail(item)">
			<my-goods :goods="item"></my-goods>
		</view>
	</view>
</template>

<script>
	/**
	 *1.下拉刷新
	 *  1.1在配置文件中开启下拉刷新
	 *      在page.json页面的配置对象中将 enablePullDownRefresh 设置为 true
	 *  1.2在页面中监听页面下拉事件
	 *      在页面的js中,跟生命周期同级位置声明 onPullDownRefresh 函数
	 *  1.3在刷新完成后关闭下来刷新效果
	 * 		   uni.stopPullDownRefresh()
	 * 2.上拉加载
	 * 				1.1箭头上拉触底
	 * 				onReachBottom
	 * 3.跳转页面
	 * 
	 */
	export default {
		data() {
			return {
				//参数对象
				params: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				total: 0, //服务器总数据量
				isLoading: false
			};
		},
		onLoad(options) {

			this.params.query = options.query || ''
			this.params.cid = options.cid || ''
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.params.pagenum = 1
			this.goodsList = []
			this.getGoodsList(uni.stopPullDownRefresh)
			//console.log(1)
		},
		onReachBottom() {
			//判断当前数据是否已经是全部数据
			if (this.params.pagenum * this.params.pagesize >= this.total) {
				return uni.$showMsg('到底了~')
			}
			if (this.isLoading) return
			//如果实在请求过程中,就不允许翻页 - 节流
			//让页数加1
			this.params.pagenum++
			this.getGoodsList()
		},
		methods: {
			//跳转到商品详情
			GoDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods-detail/goods-detail?goods_id=' + item.goods_id
				})
			},
			async getGoodsList(fn) {
				this.isLoading = true
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/goods/search', this.params)
				this.isLoading = false
				//判断是不是在下拉刷新中调用,如果是,关闭
				fn && fn()

				if (meta.status !== 200) return uni.$showMsg()
				//this.goodsList = this.goodsList.concat(message.goods)
				this.goodsList = [...this.goodsList, ...message.goods]
				this.total = message.total
			}
		}
	}
</script>

<style lang="scss">

</style>
